package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.entity.DataTheme;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.service.IDataThemeService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created by taohj on 2014/8/20.
 */
@Service
@RequestMapping("/admin/datatheme")
public class AdminDataThemeController extends AbstractAdminControllerBase {

    @Autowired
    private IDataThemeService service;

    @RequestMapping("createV")
    public String createV() {
        return "admin/datatheme/create";
    }

    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "image") MultipartFile image,
            @RequestParam(value = "description", required = false) String description
    ) {

        DataTheme theme = new DataTheme();
        theme.setName(name);
        theme.setImageFile(image);
        theme.setDescription(description);
        theme.setCreater(null);
        theme.setCreateTime(new Date());
        theme.setStatus(new SysDictionary(111));
        Result<DataTheme> result = service.create(theme,getRequest());
        return result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public String remove(
            @RequestParam("id") Integer id
    ) {
        DataTheme theme = new DataTheme();
        theme.setId(id);
        Result<DataTheme> result = service.remove(theme);
        return result.toString();
    }


    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        DataTheme theme= service.getById(id).getTarget();
        setRequestAttr("theme",theme);
        return "admin/datatheme/modify";
    }


    @ResponseBody
    @RequestMapping("modify")
    public  String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam(value = "image") MultipartFile image,
            @RequestParam("description") String description
    ){
            DataTheme theme=service.getById(id).getTarget();
            theme.setName(name);
            theme.setImageFile(image);
            theme.setDescription(description);
            theme.setUpdater(null);
            theme.setUpdateTime(new Date());
            Result<DataTheme> result=service.modify(theme,getRequest());
            return result.toString();
    }



    @RequestMapping("listV")
    public String listV(){
        return "admin/datatheme/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public  String list(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "rows",required = false) Integer size
    ){
        PageResult<DataTheme> pageResult=new PageResult<DataTheme>(page,size);
        Result<DataTheme> result=service.getByPage(pageResult);
        return result.getPageResult().toString();
    }


}
